<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <div class="article-intro" id="content">
			
        <h1>Java 运算符</h1>

<p>计算机的最基本用途之一就是执行数学运算，作为一门计算机语言，Java也提供了一套丰富的运算符来操纵变量。我们可以把运算符分成以下几组：</p>
<ul>
<li>
    算术运算符</li>
<li>
    关系运算符</li>
<li>
    位运算符</li>
<li>
    逻辑运算符</li>
<li>
    赋值运算符</li>
<li>
    其他运算符</li>
</ul>
<h2>算术运算符</h2>
<p>算术运算符用在数学表达式中，它们的作用和在数学中的作用一样。下表列出了所有的算术运算符。</p>
<p>表格中的实例假设整数变量A的值为10，变量B的值为20：</p>
<table class="reference">
<tbody>
    <tr>
        <th style="width:64px;">
        操作符</th>
        <th style="width:273px;">
        描述</th>
        <th style="width:111px;">
        例子</th>
    </tr>
    <tr>
        <td style="width:64px;">
            +</td>
        <td style="width:273px;">
            加法 - 相加运算符两侧的值</td>
        <td style="width:111px;">
            A + B 等于 30</td>
    </tr>
    <tr>
        <td style="width:64px;">
            -</td>
        <td style="width:273px;">
            减法 - 左操作数减去右操作数</td>
        <td style="width:111px;">
            A – B 等于 -10</td>
    </tr>
    <tr>
        <td style="width:64px;">
            *</td>
        <td style="width:273px;">
            乘法 - 相乘操作符两侧的值</td>
        <td style="width:111px;">
            A * B等于200</td>
    </tr>
    <tr>
        <td style="width:64px;">
            /</td>
        <td style="width:273px;">
            除法 - 左操作数除以右操作数</td>
        <td style="width:111px;">
            B / A等于2</td>
    </tr>
    <tr>
        <td style="width:64px;">
            ％</td>
        <td style="width:273px;">
            取余 - 左操作数除以右操作数的余数</td>
        <td style="width:111px;">
            B%A等于0</td>
    </tr>
    <tr>
        <td style="width:64px;">
            ++</td>
        <td style="width:273px;">
            自增: 操作数的值增加1</td>
        <td style="width:111px;">
            B++ 或 ++B 等于 21（区别详见下文）</td>
    </tr>
    <tr>
        <td style="width:64px;">
            --</td>
        <td style="width:273px;">
            自减: 操作数的值减少1</td>
        <td style="width:111px;">
            B-- 或 --B 等于 19（区别详见下文）</td>
    </tr>
</tbody>
</table>
<h3>实例</h3>
<p>下面的简单示例程序演示了算术运算符。复制并粘贴下面的 Java 程序并保存为 Test.java 文件，然后编译并运行这个程序：</p>
<div class="example"> 
<h2 class="example">实例</h2> 
<div class="example_code">
<div class="hl-main"><span class="hl-reserved">public</span><span class="hl-code"> </span><span class="hl-reserved">class</span><span class="hl-code"> </span><span class="hl-identifier">Test</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code">

</span><span class="hl-reserved">public</span><span class="hl-code"> </span><span class="hl-types">static</span><span class="hl-code"> </span><span class="hl-types">void</span><span class="hl-code"> </span><span class="hl-identifier">main</span><span class="hl-brackets">(</span><span class="hl-identifier">String</span><span class="hl-brackets">[</span><span class="hl-brackets">]</span><span class="hl-code"> </span><span class="hl-identifier">args</span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code">
 </span><span class="hl-types">int</span><span class="hl-code"> </span><span class="hl-identifier">a</span><span class="hl-code"> = </span><span class="hl-number">10</span><span class="hl-code">;
 </span><span class="hl-types">int</span><span class="hl-code"> </span><span class="hl-identifier">b</span><span class="hl-code"> = </span><span class="hl-number">20</span><span class="hl-code">;
 </span><span class="hl-types">int</span><span class="hl-code"> </span><span class="hl-identifier">c</span><span class="hl-code"> = </span><span class="hl-number">25</span><span class="hl-code">;
 </span><span class="hl-types">int</span><span class="hl-code"> </span><span class="hl-identifier">d</span><span class="hl-code"> = </span><span class="hl-number">25</span><span class="hl-code">;
 </span><span class="hl-identifier">System</span><span class="hl-code">.</span><span class="hl-identifier">out</span><span class="hl-code">.</span><span class="hl-identifier">println</span><span class="hl-brackets">(</span><span class="hl-quotes">"</span><span class="hl-string">a + b = </span><span class="hl-quotes">"</span><span class="hl-code"> + </span><span class="hl-brackets">(</span><span class="hl-identifier">a</span><span class="hl-code"> + </span><span class="hl-identifier">b</span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-brackets">)</span><span class="hl-code">;
 </span><span class="hl-identifier">System</span><span class="hl-code">.</span><span class="hl-identifier">out</span><span class="hl-code">.</span><span class="hl-identifier">println</span><span class="hl-brackets">(</span><span class="hl-quotes">"</span><span class="hl-string">a - b = </span><span class="hl-quotes">"</span><span class="hl-code"> + </span><span class="hl-brackets">(</span><span class="hl-identifier">a</span><span class="hl-code"> - </span><span class="hl-identifier">b</span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-brackets">)</span><span class="hl-code">;
 </span><span class="hl-identifier">System</span><span class="hl-code">.</span><span class="hl-identifier">out</span><span class="hl-code">.</span><span class="hl-identifier">println</span><span class="hl-brackets">(</span><span class="hl-quotes">"</span><span class="hl-string">a * b = </span><span class="hl-quotes">"</span><span class="hl-code"> + </span><span class="hl-brackets">(</span><span class="hl-identifier">a</span><span class="hl-code"> * </span><span class="hl-identifier">b</span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-brackets">)</span><span class="hl-code">;
 </span><span class="hl-identifier">System</span><span class="hl-code">.</span><span class="hl-identifier">out</span><span class="hl-code">.</span><span class="hl-identifier">println</span><span class="hl-brackets">(</span><span class="hl-quotes">"</span><span class="hl-string">b / a = </span><span class="hl-quotes">"</span><span class="hl-code"> + </span><span class="hl-brackets">(</span><span class="hl-identifier">b</span><span class="hl-code"> / </span><span class="hl-identifier">a</span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-brackets">)</span><span class="hl-code">;
 </span><span class="hl-identifier">System</span><span class="hl-code">.</span><span class="hl-identifier">out</span><span class="hl-code">.</span><span class="hl-identifier">println</span><span class="hl-brackets">(</span><span class="hl-quotes">"</span><span class="hl-string">b % a = </span><span class="hl-quotes">"</span><span class="hl-code"> + </span><span class="hl-brackets">(</span><span class="hl-identifier">b</span><span class="hl-code"> % </span><span class="hl-identifier">a</span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-brackets">)</span><span class="hl-code">;
 </span><span class="hl-identifier">System</span><span class="hl-code">.</span><span class="hl-identifier">out</span><span class="hl-code">.</span><span class="hl-identifier">println</span><span class="hl-brackets">(</span><span class="hl-quotes">"</span><span class="hl-string">c % a = </span><span class="hl-quotes">"</span><span class="hl-code"> + </span><span class="hl-brackets">(</span><span class="hl-identifier">c</span><span class="hl-code"> % </span><span class="hl-identifier">a</span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-brackets">)</span><span class="hl-code">;
 </span><span class="hl-identifier">System</span><span class="hl-code">.</span><span class="hl-identifier">out</span><span class="hl-code">.</span><span class="hl-identifier">println</span><span class="hl-brackets">(</span><span class="hl-quotes">"</span><span class="hl-string">a++   = </span><span class="hl-quotes">"</span><span class="hl-code"> +  </span><span class="hl-brackets">(</span><span class="hl-identifier">a</span><span class="hl-code">++</span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-brackets">)</span><span class="hl-code">;
 </span><span class="hl-identifier">System</span><span class="hl-code">.</span><span class="hl-identifier">out</span><span class="hl-code">.</span><span class="hl-identifier">println</span><span class="hl-brackets">(</span><span class="hl-quotes">"</span><span class="hl-string">a--   = </span><span class="hl-quotes">"</span><span class="hl-code"> +  </span><span class="hl-brackets">(</span><span class="hl-identifier">a</span><span class="hl-code">--</span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-brackets">)</span><span class="hl-code">;
 </span><span class="hl-comment">//</span><span class="hl-comment"> 查看  d++ 与 ++d 的不同</span><span class="hl-comment"></span><span class="hl-code">
 </span><span class="hl-identifier">System</span><span class="hl-code">.</span><span class="hl-identifier">out</span><span class="hl-code">.</span><span class="hl-identifier">println</span><span class="hl-brackets">(</span><span class="hl-quotes">"</span><span class="hl-string">d++   = </span><span class="hl-quotes">"</span><span class="hl-code"> +  </span><span class="hl-brackets">(</span><span class="hl-identifier">d</span><span class="hl-code">++</span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-brackets">)</span><span class="hl-code">;
 </span><span class="hl-identifier">System</span><span class="hl-code">.</span><span class="hl-identifier">out</span><span class="hl-code">.</span><span class="hl-identifier">println</span><span class="hl-brackets">(</span><span class="hl-quotes">"</span><span class="hl-string">++d   = </span><span class="hl-quotes">"</span><span class="hl-code"> +  </span><span class="hl-brackets">(</span><span class="hl-code">++</span><span class="hl-identifier">d</span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-brackets">)</span><span class="hl-code">;
</span><span class="hl-brackets">}</span><span class="hl-code">
</span><span class="hl-brackets">}</span></div>
<button class="copy-code-button" type="button" data-clipboard-text="public class Test {

public static void main(String[] args) {
 int a = 10;
 int b = 20;
 int c = 25;
 int d = 25;
 System.out.println(&quot;a + b = &quot; + (a + b) );
 System.out.println(&quot;a - b = &quot; + (a - b) );
 System.out.println(&quot;a * b = &quot; + (a * b) );
 System.out.println(&quot;b / a = &quot; + (b / a) );
 System.out.println(&quot;b % a = &quot; + (b % a) );
 System.out.println(&quot;c % a = &quot; + (c % a) );
 System.out.println(&quot;a++   = &quot; +  (a++) );
 System.out.println(&quot;a--   = &quot; +  (a--) );
 // 查看  d++ 与 ++d 的不同
 System.out.println(&quot;d++   = &quot; +  (d++) );
 System.out.println(&quot;++d   = &quot; +  (++d) );
}
}"></button></div><br>
<a target="_blank" href="/try/showjava.php?filename=test_operator" class="showbtn" rel="noopener noreferrer">运行实例 »</a>
</div>
<p>以上实例编译运行结果如下：</p>
<pre class="prettyprint prettyprinted" style=""><span class="pln">a </span><span class="pun">+</span><span class="pln"> b </span><span class="pun">=</span><span class="pln"> </span><span class="lit">30</span><span class="pln">
a </span><span class="pun">-</span><span class="pln"> b </span><span class="pun">=</span><span class="pln"> </span><span class="pun">-</span><span class="lit">10</span><span class="pln">
a </span><span class="pun">*</span><span class="pln"> b </span><span class="pun">=</span><span class="pln"> </span><span class="lit">200</span><span class="pln">
b </span><span class="pun">/</span><span class="pln"> a </span><span class="pun">=</span><span class="pln"> </span><span class="lit">2</span><span class="pln">
b </span><span class="pun">%</span><span class="pln"> a </span><span class="pun">=</span><span class="pln"> </span><span class="lit">0</span><span class="pln">
c </span><span class="pun">%</span><span class="pln"> a </span><span class="pun">=</span><span class="pln"> </span><span class="lit">5</span><span class="pln">
a</span><span class="pun">++</span><span class="pln">   </span><span class="pun">=</span><span class="pln"> </span><span class="lit">10</span><span class="pln">
a</span><span class="pun">--</span><span class="pln">   </span><span class="pun">=</span><span class="pln"> </span><span class="lit">11</span><span class="pln">
d</span><span class="pun">++</span><span class="pln">   </span><span class="pun">=</span><span class="pln"> </span><span class="lit">25</span><span class="pln">
</span><span class="pun">++</span><span class="pln">d   </span><span class="pun">=</span><span class="pln"> </span><span class="lit">27</span><button class="copy-code-button" type="button" data-clipboard-text="a + b = 30
a - b = -10
a * b = 200
b / a = 2
b % a = 0
c % a = 5
a++   = 10
a--   = 11
d++   = 25
++d   = 27
"></button></pre>
<h3>自增自减运算符</h3>
<p><strong>1、自增（++）自减（--）运算符</strong>是一种特殊的算术运算符，在算术运算符中需要两个操作数来进行运算，而自增自减运算符是一个操作数。</p>
<div class="example"> 
<h2 class="example">实例</h2> 
<div class="example_code">
<div class="hl-main"><span class="hl-reserved">public</span><span class="hl-code"> </span><span class="hl-reserved">class</span><span class="hl-code"> </span><span class="hl-identifier">selfAddMinus</span><span class="hl-brackets">{</span><span class="hl-code">
</span><span class="hl-reserved">public</span><span class="hl-code"> </span><span class="hl-types">static</span><span class="hl-code"> </span><span class="hl-types">void</span><span class="hl-code"> </span><span class="hl-identifier">main</span><span class="hl-brackets">(</span><span class="hl-identifier">String</span><span class="hl-brackets">[</span><span class="hl-brackets">]</span><span class="hl-code"> </span><span class="hl-identifier">args</span><span class="hl-brackets">)</span><span class="hl-brackets">{</span><span class="hl-code">
    </span><span class="hl-types">int</span><span class="hl-code"> </span><span class="hl-identifier">a</span><span class="hl-code"> = </span><span class="hl-number">3</span><span class="hl-code">;</span><span class="hl-comment">//</span><span class="hl-comment">定义一个变量；</span><span class="hl-comment"></span><span class="hl-code">
    </span><span class="hl-types">int</span><span class="hl-code"> </span><span class="hl-identifier">b</span><span class="hl-code"> = ++</span><span class="hl-identifier">a</span><span class="hl-code">;</span><span class="hl-comment">//</span><span class="hl-comment">自增运算</span><span class="hl-comment"></span><span class="hl-code">
    </span><span class="hl-types">int</span><span class="hl-code"> </span><span class="hl-identifier">c</span><span class="hl-code"> = </span><span class="hl-number">3</span><span class="hl-code">;
    </span><span class="hl-types">int</span><span class="hl-code"> </span><span class="hl-identifier">d</span><span class="hl-code"> = --</span><span class="hl-identifier">c</span><span class="hl-code">;</span><span class="hl-comment">//</span><span class="hl-comment">自减运算</span><span class="hl-comment"></span><span class="hl-code">
    </span><span class="hl-identifier">System</span><span class="hl-code">.</span><span class="hl-identifier">out</span><span class="hl-code">.</span><span class="hl-identifier">println</span><span class="hl-brackets">(</span><span class="hl-quotes">"</span><span class="hl-string">进行自增运算后的值等于</span><span class="hl-quotes">"</span><span class="hl-code">+</span><span class="hl-identifier">b</span><span class="hl-brackets">)</span><span class="hl-code">;
    </span><span class="hl-identifier">System</span><span class="hl-code">.</span><span class="hl-identifier">out</span><span class="hl-code">.</span><span class="hl-identifier">println</span><span class="hl-brackets">(</span><span class="hl-quotes">"</span><span class="hl-string">进行自减运算后的值等于</span><span class="hl-quotes">"</span><span class="hl-code">+</span><span class="hl-identifier">d</span><span class="hl-brackets">)</span><span class="hl-code">;
</span><span class="hl-brackets">}</span><span class="hl-code">
</span><span class="hl-brackets">}</span></div>
<button class="copy-code-button" type="button" data-clipboard-text="public class selfAddMinus{
public static void main(String[] args){
    int a = 3;//定义一个变量；
    int b = ++a;//自增运算
    int c = 3;
    int d = --c;//自减运算
    System.out.println(&quot;进行自增运算后的值等于&quot;+b);
    System.out.println(&quot;进行自减运算后的值等于&quot;+d);
}
}"></button></div>
</div>
<p>运行结果为：</p>
<pre class="prettyprint prettyprinted" style=""><span class="pun">进行自增运算后的值等于</span><span class="lit">4</span><span class="pln">
</span><span class="pun">进行自减运算后的值等于</span><span class="lit">2</span><button class="copy-code-button" type="button" data-clipboard-text="进行自增运算后的值等于4
进行自减运算后的值等于2"></button></pre>
<p>解析：</p>
<ul>
<li><p>int b = ++a; 拆分运算过程为: a=a+1=4; b=a=4, 最后结果为b=4,a=4</p>
</li><li><p>
int d = --c; 拆分运算过程为: c=c-1=2; d=c=2, 最后结果为d=2,c=2</p></li>
</ul>
<p><strong>2、前缀自增自减法(++a,--a): </strong>先进行自增或者自减运算，再进行表达式运算。</p><p></p>
<p><strong>3、后缀自增自减法(a++,a--): </strong>先进行表达式运算，再进行自增或者自减运算
实例：</p>
<div class="example"> 
<h2 class="example">实例</h2> 
<div class="example_code">
<div class="hl-main"><span class="hl-reserved">public</span><span class="hl-code"> </span><span class="hl-reserved">class</span><span class="hl-code"> </span><span class="hl-identifier">selfAddMinus</span><span class="hl-brackets">{</span><span class="hl-code">
</span><span class="hl-reserved">public</span><span class="hl-code"> </span><span class="hl-types">static</span><span class="hl-code"> </span><span class="hl-types">void</span><span class="hl-code"> </span><span class="hl-identifier">main</span><span class="hl-brackets">(</span><span class="hl-identifier">String</span><span class="hl-brackets">[</span><span class="hl-brackets">]</span><span class="hl-code"> </span><span class="hl-identifier">args</span><span class="hl-brackets">)</span><span class="hl-brackets">{</span><span class="hl-code">
    </span><span class="hl-types">int</span><span class="hl-code"> </span><span class="hl-identifier">a</span><span class="hl-code"> = </span><span class="hl-number">5</span><span class="hl-code">;</span><span class="hl-comment">//</span><span class="hl-comment">定义一个变量；</span><span class="hl-comment"></span><span class="hl-code">
    </span><span class="hl-types">int</span><span class="hl-code"> </span><span class="hl-identifier">b</span><span class="hl-code"> = </span><span class="hl-number">5</span><span class="hl-code">;
    </span><span class="hl-types">int</span><span class="hl-code"> </span><span class="hl-identifier">x</span><span class="hl-code"> = </span><span class="hl-number">2</span><span class="hl-code">*++</span><span class="hl-identifier">a</span><span class="hl-code">;
    </span><span class="hl-types">int</span><span class="hl-code"> </span><span class="hl-identifier">y</span><span class="hl-code"> = </span><span class="hl-number">2</span><span class="hl-code">*</span><span class="hl-identifier">b</span><span class="hl-code">++;
    </span><span class="hl-identifier">System</span><span class="hl-code">.</span><span class="hl-identifier">out</span><span class="hl-code">.</span><span class="hl-identifier">println</span><span class="hl-brackets">(</span><span class="hl-quotes">"</span><span class="hl-string">自增运算符前缀运算后a=</span><span class="hl-quotes">"</span><span class="hl-code">+</span><span class="hl-identifier">a</span><span class="hl-code">+</span><span class="hl-quotes">"</span><span class="hl-string">,x=</span><span class="hl-quotes">"</span><span class="hl-code">+</span><span class="hl-identifier">x</span><span class="hl-brackets">)</span><span class="hl-code">;
    </span><span class="hl-identifier">System</span><span class="hl-code">.</span><span class="hl-identifier">out</span><span class="hl-code">.</span><span class="hl-identifier">println</span><span class="hl-brackets">(</span><span class="hl-quotes">"</span><span class="hl-string">自增运算符后缀运算后b=</span><span class="hl-quotes">"</span><span class="hl-code">+</span><span class="hl-identifier">b</span><span class="hl-code">+</span><span class="hl-quotes">"</span><span class="hl-string">,y=</span><span class="hl-quotes">"</span><span class="hl-code">+</span><span class="hl-identifier">y</span><span class="hl-brackets">)</span><span class="hl-code">;
</span><span class="hl-brackets">}</span><span class="hl-code">
</span><span class="hl-brackets">}</span></div>
<button class="copy-code-button" type="button" data-clipboard-text="public class selfAddMinus{
public static void main(String[] args){
    int a = 5;//定义一个变量；
    int b = 5;
    int x = 2*++a;
    int y = 2*b++;
    System.out.println(&quot;自增运算符前缀运算后a=&quot;+a+&quot;,x=&quot;+x);
    System.out.println(&quot;自增运算符后缀运算后b=&quot;+b+&quot;,y=&quot;+y);
}
}"></button></div>
</div>
<p>运行结果为：</p>
<pre class="prettyprint prettyprinted" style=""><span class="pun">自增运算符前缀运算后</span><span class="pln">a</span><span class="pun">=</span><span class="lit">6</span><span class="pun">，</span><span class="pln">x</span><span class="pun">=</span><span class="lit">12</span><span class="pln">
</span><span class="pun">自增运算符后缀运算后</span><span class="pln">b</span><span class="pun">=</span><span class="lit">6</span><span class="pun">，</span><span class="pln">y</span><span class="pun">=</span><span class="lit">10</span><button class="copy-code-button" type="button" data-clipboard-text="自增运算符前缀运算后a=6，x=12
自增运算符后缀运算后b=6，y=10"></button></pre>
<hr>
<h2>关系运算符</h2>

<p>下表为Java支持的关系运算符</p>
<p>表格中的实例整数变量A的值为10，变量B的值为20：</p>

<table class="reference">
<tbody>
    <tr>
        <th style="width:42px;">
            运算符</th>
        <th style="width:407px;">
            描述</th>
        <th style="width:128px;">
            例子</th>
    </tr>
    <tr>
        <td style="width:42px;">
            ==</td>
        <td style="width:407px;">
            检查如果两个操作数的值是否相等，如果相等则条件为真。</td>
        <td style="width:128px;">
            （A == B）为假。</td>
    </tr>
    <tr>
        <td style="width:42px;">
            !=</td>
        <td style="width:407px;">
            检查如果两个操作数的值是否相等，如果值不相等则条件为真。</td>
        <td style="width:128px;">
            (A != B) 为真。</td>
    </tr>
    <tr>
        <td style="width:42px;">
            &gt;&nbsp;</td>
        <td style="width:407px;">
            检查左操作数的值是否大于右操作数的值，如果是那么条件为真。</td>
        <td style="width:128px;">
            （A&gt; B）为假。</td>
    </tr>
    <tr>
        <td style="width:42px;">
            &lt;&nbsp;</td>
        <td style="width:407px;">
            检查左操作数的值是否小于右操作数的值，如果是那么条件为真。</td>
        <td style="width:128px;">
            （A &lt;B）为真。</td>
    </tr>
    <tr>
        <td style="width:42px;">
            &gt;=</td>
        <td style="width:407px;">
            检查左操作数的值是否大于或等于右操作数的值，如果是那么条件为真。</td>
        <td style="width:128px;">
            （A&gt; = B）为假。</td>
    </tr>
    <tr>
        <td style="width:42px;">
            &lt;=</td>
        <td style="width:407px;">
            检查左操作数的值是否小于或等于右操作数的值，如果是那么条件为真。</td>
        <td style="width:128px;">
            （A &lt;= B）为真。</td>
    </tr>
</tbody>
</table>

<h3>实例</h3>
<p>下面的简单示例程序演示了关系运算符。复制并粘贴下面的Java程序并保存为Test.java文件，然后编译并运行这个程序：</p>
<div class="example">
<h2 class="example">Test.java 文件代码：</h2> 
<div class="example_code">
<div class="hl-main"><span class="hl-reserved">public</span><span class="hl-code"> </span><span class="hl-reserved">class</span><span class="hl-code"> </span><span class="hl-identifier">Test</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code">

</span><span class="hl-reserved">public</span><span class="hl-code"> </span><span class="hl-types">static</span><span class="hl-code"> </span><span class="hl-types">void</span><span class="hl-code"> </span><span class="hl-identifier">main</span><span class="hl-brackets">(</span><span class="hl-identifier">String</span><span class="hl-brackets">[</span><span class="hl-brackets">]</span><span class="hl-code"> </span><span class="hl-identifier">args</span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code">
 </span><span class="hl-types">int</span><span class="hl-code"> </span><span class="hl-identifier">a</span><span class="hl-code"> = </span><span class="hl-number">10</span><span class="hl-code">;
 </span><span class="hl-types">int</span><span class="hl-code"> </span><span class="hl-identifier">b</span><span class="hl-code"> = </span><span class="hl-number">20</span><span class="hl-code">;
 </span><span class="hl-identifier">System</span><span class="hl-code">.</span><span class="hl-identifier">out</span><span class="hl-code">.</span><span class="hl-identifier">println</span><span class="hl-brackets">(</span><span class="hl-quotes">"</span><span class="hl-string">a == b = </span><span class="hl-quotes">"</span><span class="hl-code"> + </span><span class="hl-brackets">(</span><span class="hl-identifier">a</span><span class="hl-code"> == </span><span class="hl-identifier">b</span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-brackets">)</span><span class="hl-code">;
 </span><span class="hl-identifier">System</span><span class="hl-code">.</span><span class="hl-identifier">out</span><span class="hl-code">.</span><span class="hl-identifier">println</span><span class="hl-brackets">(</span><span class="hl-quotes">"</span><span class="hl-string">a != b = </span><span class="hl-quotes">"</span><span class="hl-code"> + </span><span class="hl-brackets">(</span><span class="hl-identifier">a</span><span class="hl-code"> != </span><span class="hl-identifier">b</span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-brackets">)</span><span class="hl-code">;
 </span><span class="hl-identifier">System</span><span class="hl-code">.</span><span class="hl-identifier">out</span><span class="hl-code">.</span><span class="hl-identifier">println</span><span class="hl-brackets">(</span><span class="hl-quotes">"</span><span class="hl-string">a &gt; b = </span><span class="hl-quotes">"</span><span class="hl-code"> + </span><span class="hl-brackets">(</span><span class="hl-identifier">a</span><span class="hl-code"> &gt; </span><span class="hl-identifier">b</span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-brackets">)</span><span class="hl-code">;
 </span><span class="hl-identifier">System</span><span class="hl-code">.</span><span class="hl-identifier">out</span><span class="hl-code">.</span><span class="hl-identifier">println</span><span class="hl-brackets">(</span><span class="hl-quotes">"</span><span class="hl-string">a &lt; b = </span><span class="hl-quotes">"</span><span class="hl-code"> + </span><span class="hl-brackets">(</span><span class="hl-identifier">a</span><span class="hl-code"> &lt; </span><span class="hl-identifier">b</span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-brackets">)</span><span class="hl-code">;
 </span><span class="hl-identifier">System</span><span class="hl-code">.</span><span class="hl-identifier">out</span><span class="hl-code">.</span><span class="hl-identifier">println</span><span class="hl-brackets">(</span><span class="hl-quotes">"</span><span class="hl-string">b &gt;= a = </span><span class="hl-quotes">"</span><span class="hl-code"> + </span><span class="hl-brackets">(</span><span class="hl-identifier">b</span><span class="hl-code"> &gt;= </span><span class="hl-identifier">a</span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-brackets">)</span><span class="hl-code">;
 </span><span class="hl-identifier">System</span><span class="hl-code">.</span><span class="hl-identifier">out</span><span class="hl-code">.</span><span class="hl-identifier">println</span><span class="hl-brackets">(</span><span class="hl-quotes">"</span><span class="hl-string">b &lt;= a = </span><span class="hl-quotes">"</span><span class="hl-code"> + </span><span class="hl-brackets">(</span><span class="hl-identifier">b</span><span class="hl-code"> &lt;= </span><span class="hl-identifier">a</span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-brackets">)</span><span class="hl-code">;
</span><span class="hl-brackets">}</span><span class="hl-code">
</span><span class="hl-brackets">}</span></div>
<button class="copy-code-button" type="button" data-clipboard-text="public class Test {

public static void main(String[] args) {
 int a = 10;
 int b = 20;
 System.out.println(&quot;a == b = &quot; + (a == b) );
 System.out.println(&quot;a != b = &quot; + (a != b) );
 System.out.println(&quot;a > b = &quot; + (a > b) );
 System.out.println(&quot;a < b = &quot; + (a < b) );
 System.out.println(&quot;b >= a = &quot; + (b >= a) );
 System.out.println(&quot;b <= a = &quot; + (b <= a) );
}
}"></button></div>
</div>

<p>以上实例编译运行结果如下：</p>
<pre class="prettyprint prettyprinted" style=""><span class="pln">a </span><span class="pun">==</span><span class="pln"> b </span><span class="pun">=</span><span class="pln"> </span><span class="kwd">false</span><span class="pln">
a </span><span class="pun">!=</span><span class="pln"> b </span><span class="pun">=</span><span class="pln"> </span><span class="kwd">true</span><span class="pln">
a </span><span class="pun">&gt;</span><span class="pln"> b </span><span class="pun">=</span><span class="pln"> </span><span class="kwd">false</span><span class="pln">
a </span><span class="pun">&lt;</span><span class="pln"> b </span><span class="pun">=</span><span class="pln"> </span><span class="kwd">true</span><span class="pln">
b </span><span class="pun">&gt;=</span><span class="pln"> a </span><span class="pun">=</span><span class="pln"> </span><span class="kwd">true</span><span class="pln">
b </span><span class="pun">&lt;=</span><span class="pln"> a </span><span class="pun">=</span><span class="pln"> </span><span class="kwd">false</span><button class="copy-code-button" type="button" data-clipboard-text="a == b = false
a != b = true
a > b = false
a < b = true
b >= a = true
b <= a = false
"></button></pre>
<hr><h2>
位运算符</h2>
<p>

Java定义了位运算符，应用于整数类型(int)，长整型(long)，短整型(short)，字符型(char)，和字节型(byte)等类型。
</p>
<p>
位运算符作用在所有的位上，并且按位运算。假设a = 60，b = 13;它们的二进制格式表示将如下：</p>
<pre class="prettyprint prettyprinted" style=""><span class="pln">A </span><span class="pun">=</span><span class="pln"> </span><span class="lit">0011</span><span class="pln"> </span><span class="lit">1100</span><span class="pln">
B </span><span class="pun">=</span><span class="pln"> </span><span class="lit">0000</span><span class="pln"> </span><span class="lit">1101</span><span class="pln">
</span><span class="pun">-----------------</span><span class="pln">
A</span><span class="pun">&amp;</span><span class="pln">B </span><span class="pun">=</span><span class="pln"> </span><span class="lit">0000</span><span class="pln"> </span><span class="lit">1100</span><span class="pln">
A </span><span class="pun">|</span><span class="pln"> B </span><span class="pun">=</span><span class="pln"> </span><span class="lit">0011</span><span class="pln"> </span><span class="lit">1101</span><span class="pln">
A </span><span class="pun">^</span><span class="pln"> B </span><span class="pun">=</span><span class="pln"> </span><span class="lit">0011</span><span class="pln"> </span><span class="lit">0001</span><span class="pln">
</span><span class="pun">~</span><span class="pln">A</span><span class="pun">=</span><span class="pln"> </span><span class="lit">1100</span><span class="pln"> </span><span class="lit">0011</span><button class="copy-code-button" type="button" data-clipboard-text="A = 0011 1100
B = 0000 1101
-----------------
A&amp;B = 0000 1100
A | B = 0011 1101
A ^ B = 0011 0001
~A= 1100 0011
"></button></pre>
<p>下表列出了位运算符的基本运算，假设整数变量 A 的值为 60 和变量 B 的值为 13：</p>
<table class="reference">
<tbody>
    <tr>
        <th style="width:57px;">
            操作符</th>
        <th style="width:372px;">
            描述</th>
        <th style="width:148px;">
            例子</th>
    </tr>
    <tr>
        <td style="width:57px;">
            ＆</td>
        <td style="width:372px;">
            如果相对应位都是1，则结果为1，否则为0</td>
        <td style="width:148px;">
            （A＆B），得到12，即0000 1100</td>
    </tr>
    <tr>
        <td style="width:57px;">
            |</td>
        <td style="width:372px;">
            如果相对应位都是 0，则结果为 0，否则为 1</td>
        <td style="width:148px;">
            （A | B）得到61，即 0011 1101</td>
    </tr>
    <tr>
        <td style="width:57px;">
            ^</td>
        <td style="width:372px;">
            如果相对应位值相同，则结果为0，否则为1</td>
        <td style="width:148px;">
            （A ^ B）得到49，即 0011 0001</td>
    </tr>
    <tr>
        <td style="width:57px;">
            〜</td>
        <td style="width:372px;">
            按位取反运算符翻转操作数的每一位，即0变成1，1变成0。</td>
        <td style="width:148px;">
            （〜A）得到-61，即1100 0011</td>
    </tr>
    <tr>
        <td style="width:57px;">
            &lt;&lt;&nbsp;</td>
        <td style="width:372px;">
            按位左移运算符。左操作数按位左移右操作数指定的位数。</td>
        <td style="width:148px;">
            A &lt;&lt; 2得到240，即 1111 0000</td>
    </tr>
    <tr>
        <td style="width:57px;">
            &gt;&gt;&nbsp;</td>
        <td style="width:372px;">
            按位右移运算符。左操作数按位右移右操作数指定的位数。</td>
        <td style="width:148px;">
            A &gt;&gt; 2得到15即 1111</td>
    </tr>
    <tr>
        <td style="width:57px;">
            &gt;&gt;&gt;&nbsp;</td>
        <td style="width:372px;">
            按位右移补零操作符。左操作数的值按右操作数指定的位数右移，移动得到的空位以零填充。</td>
        <td style="width:148px;">
            A&gt;&gt;&gt;2得到15即0000 1111</td>
    </tr>
</tbody>
</table>
<h3>实例</h3>
<p>下面的简单示例程序演示了位运算符。复制并粘贴下面的Java程序并保存为Test.java文件，然后编译并运行这个程序：
</p>
<div class="example">
<h2 class="example">Test.java 文件代码：</h2> 
<div class="example_code">
<div class="hl-main"><span class="hl-reserved">public</span><span class="hl-code"> </span><span class="hl-reserved">class</span><span class="hl-code"> </span><span class="hl-identifier">Test</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code">
&nbsp; </span><span class="hl-reserved">public</span><span class="hl-code"> </span><span class="hl-types">static</span><span class="hl-code"> </span><span class="hl-types">void</span><span class="hl-code"> </span><span class="hl-identifier">main</span><span class="hl-brackets">(</span><span class="hl-identifier">String</span><span class="hl-brackets">[</span><span class="hl-brackets">]</span><span class="hl-code"> </span><span class="hl-identifier">args</span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code">
&nbsp;&nbsp;&nbsp;  </span><span class="hl-types">int</span><span class="hl-code"> </span><span class="hl-identifier">a</span><span class="hl-code"> = </span><span class="hl-number">60</span><span class="hl-code">; </span><span class="hl-comment">/*</span><span class="hl-comment"> 60 = 0011 1100 </span><span class="hl-comment">*/</span><span class="hl-code">&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp; </span><span class="hl-types">int</span><span class="hl-code"> </span><span class="hl-identifier">b</span><span class="hl-code"> = </span><span class="hl-number">13</span><span class="hl-code">; </span><span class="hl-comment">/*</span><span class="hl-comment"> 13 = 0000 1101 </span><span class="hl-comment">*/</span><span class="hl-code">
&nbsp;&nbsp;&nbsp;&nbsp; </span><span class="hl-types">int</span><span class="hl-code"> </span><span class="hl-identifier">c</span><span class="hl-code"> = </span><span class="hl-number">0</span><span class="hl-code">;
&nbsp;&nbsp;&nbsp;&nbsp; </span><span class="hl-identifier">c</span><span class="hl-code"> = </span><span class="hl-identifier">a</span><span class="hl-code"> &amp; </span><span class="hl-identifier">b</span><span class="hl-code">;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span class="hl-comment">/*</span><span class="hl-comment"> 12 = 0000 1100 </span><span class="hl-comment">*/</span><span class="hl-code">
&nbsp;&nbsp;&nbsp;&nbsp; </span><span class="hl-identifier">System</span><span class="hl-code">.</span><span class="hl-identifier">out</span><span class="hl-code">.</span><span class="hl-identifier">println</span><span class="hl-brackets">(</span><span class="hl-quotes">"</span><span class="hl-string">a &amp; b = </span><span class="hl-quotes">"</span><span class="hl-code"> + </span><span class="hl-identifier">c</span><span class="hl-code"> </span><span class="hl-brackets">)</span><span class="hl-code">;

&nbsp;&nbsp;&nbsp;&nbsp; </span><span class="hl-identifier">c</span><span class="hl-code"> = </span><span class="hl-identifier">a</span><span class="hl-code"> | </span><span class="hl-identifier">b</span><span class="hl-code">;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span class="hl-comment">/*</span><span class="hl-comment"> 61 = 0011 1101 </span><span class="hl-comment">*/</span><span class="hl-code">
&nbsp;&nbsp;&nbsp;&nbsp; </span><span class="hl-identifier">System</span><span class="hl-code">.</span><span class="hl-identifier">out</span><span class="hl-code">.</span><span class="hl-identifier">println</span><span class="hl-brackets">(</span><span class="hl-quotes">"</span><span class="hl-string">a | b = </span><span class="hl-quotes">"</span><span class="hl-code"> + </span><span class="hl-identifier">c</span><span class="hl-code"> </span><span class="hl-brackets">)</span><span class="hl-code">;

&nbsp;&nbsp;&nbsp;&nbsp; </span><span class="hl-identifier">c</span><span class="hl-code"> = </span><span class="hl-identifier">a</span><span class="hl-code"> ^ </span><span class="hl-identifier">b</span><span class="hl-code">;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span class="hl-comment">/*</span><span class="hl-comment"> 49 = 0011 0001 </span><span class="hl-comment">*/</span><span class="hl-code">
&nbsp;&nbsp;&nbsp;&nbsp; </span><span class="hl-identifier">System</span><span class="hl-code">.</span><span class="hl-identifier">out</span><span class="hl-code">.</span><span class="hl-identifier">println</span><span class="hl-brackets">(</span><span class="hl-quotes">"</span><span class="hl-string">a ^ b = </span><span class="hl-quotes">"</span><span class="hl-code"> + </span><span class="hl-identifier">c</span><span class="hl-code"> </span><span class="hl-brackets">)</span><span class="hl-code">;

&nbsp;&nbsp;&nbsp;&nbsp; </span><span class="hl-identifier">c</span><span class="hl-code"> = ~</span><span class="hl-identifier">a</span><span class="hl-code">;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span class="hl-comment">/*</span><span class="hl-comment">-61 = 1100 0011 </span><span class="hl-comment">*/</span><span class="hl-code">
&nbsp;&nbsp;&nbsp;&nbsp; </span><span class="hl-identifier">System</span><span class="hl-code">.</span><span class="hl-identifier">out</span><span class="hl-code">.</span><span class="hl-identifier">println</span><span class="hl-brackets">(</span><span class="hl-quotes">"</span><span class="hl-string">~a = </span><span class="hl-quotes">"</span><span class="hl-code"> + </span><span class="hl-identifier">c</span><span class="hl-code"> </span><span class="hl-brackets">)</span><span class="hl-code">;

&nbsp;&nbsp;&nbsp;&nbsp; </span><span class="hl-identifier">c</span><span class="hl-code"> = </span><span class="hl-identifier">a</span><span class="hl-code"> &lt;&lt; </span><span class="hl-number">2</span><span class="hl-code">;&nbsp;&nbsp;&nbsp;&nbsp; </span><span class="hl-comment">/*</span><span class="hl-comment"> 240 = 1111 0000 </span><span class="hl-comment">*/</span><span class="hl-code">
&nbsp;&nbsp;&nbsp;&nbsp; </span><span class="hl-identifier">System</span><span class="hl-code">.</span><span class="hl-identifier">out</span><span class="hl-code">.</span><span class="hl-identifier">println</span><span class="hl-brackets">(</span><span class="hl-quotes">"</span><span class="hl-string">a &lt;&lt; 2 = </span><span class="hl-quotes">"</span><span class="hl-code"> + </span><span class="hl-identifier">c</span><span class="hl-code"> </span><span class="hl-brackets">)</span><span class="hl-code">;

&nbsp;&nbsp;&nbsp;&nbsp; </span><span class="hl-identifier">c</span><span class="hl-code"> = </span><span class="hl-identifier">a</span><span class="hl-code"> &gt;&gt; </span><span class="hl-number">2</span><span class="hl-code">;&nbsp;&nbsp;&nbsp;&nbsp; </span><span class="hl-comment">/*</span><span class="hl-comment"> 15 = 1111 </span><span class="hl-comment">*/</span><span class="hl-code">
&nbsp;&nbsp;&nbsp;&nbsp; </span><span class="hl-identifier">System</span><span class="hl-code">.</span><span class="hl-identifier">out</span><span class="hl-code">.</span><span class="hl-identifier">println</span><span class="hl-brackets">(</span><span class="hl-quotes">"</span><span class="hl-string">a &gt;&gt; 2&nbsp; = </span><span class="hl-quotes">"</span><span class="hl-code"> + </span><span class="hl-identifier">c</span><span class="hl-code"> </span><span class="hl-brackets">)</span><span class="hl-code">;
&nbsp;&nbsp;
&nbsp;&nbsp; </span><span class="hl-identifier">c</span><span class="hl-code"> = </span><span class="hl-identifier">a</span><span class="hl-code"> &gt;&gt;&gt; </span><span class="hl-number">2</span><span class="hl-code">;&nbsp;&nbsp;&nbsp;&nbsp; </span><span class="hl-comment">/*</span><span class="hl-comment"> 15 = 0000 1111 </span><span class="hl-comment">*/</span><span class="hl-code">
&nbsp;&nbsp;&nbsp;&nbsp; </span><span class="hl-identifier">System</span><span class="hl-code">.</span><span class="hl-identifier">out</span><span class="hl-code">.</span><span class="hl-identifier">println</span><span class="hl-brackets">(</span><span class="hl-quotes">"</span><span class="hl-string">a &gt;&gt;&gt; 2 = </span><span class="hl-quotes">"</span><span class="hl-code"> + </span><span class="hl-identifier">c</span><span class="hl-code"> </span><span class="hl-brackets">)</span><span class="hl-code">;
&nbsp; </span><span class="hl-brackets">}</span><span class="hl-code">
</span><span class="hl-brackets">}</span><span class="hl-code">&nbsp;</span></div>
<button class="copy-code-button" type="button" data-clipboard-text="public class Test {
&nbsp; public static void main(String[] args) {
&nbsp;&nbsp;&nbsp;  int a = 60; /* 60 = 0011 1100 */&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp; int b = 13; /* 13 = 0000 1101 */
&nbsp;&nbsp;&nbsp;&nbsp; int c = 0;
&nbsp;&nbsp;&nbsp;&nbsp; c = a &amp; b;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; /* 12 = 0000 1100 */
&nbsp;&nbsp;&nbsp;&nbsp; System.out.println(&quot;a &amp; b = &quot; + c );

&nbsp;&nbsp;&nbsp;&nbsp; c = a | b;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; /* 61 = 0011 1101 */
&nbsp;&nbsp;&nbsp;&nbsp; System.out.println(&quot;a | b = &quot; + c );

&nbsp;&nbsp;&nbsp;&nbsp; c = a ^ b;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; /* 49 = 0011 0001 */
&nbsp;&nbsp;&nbsp;&nbsp; System.out.println(&quot;a ^ b = &quot; + c );

&nbsp;&nbsp;&nbsp;&nbsp; c = ~a;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; /*-61 = 1100 0011 */
&nbsp;&nbsp;&nbsp;&nbsp; System.out.println(&quot;~a = &quot; + c );

&nbsp;&nbsp;&nbsp;&nbsp; c = a << 2;&nbsp;&nbsp;&nbsp;&nbsp; /* 240 = 1111 0000 */
&nbsp;&nbsp;&nbsp;&nbsp; System.out.println(&quot;a << 2 = &quot; + c );

&nbsp;&nbsp;&nbsp;&nbsp; c = a >> 2;&nbsp;&nbsp;&nbsp;&nbsp; /* 15 = 1111 */
&nbsp;&nbsp;&nbsp;&nbsp; System.out.println(&quot;a >> 2&nbsp; = &quot; + c );
&nbsp;&nbsp;
&nbsp;&nbsp; c = a >>> 2;&nbsp;&nbsp;&nbsp;&nbsp; /* 15 = 0000 1111 */
&nbsp;&nbsp;&nbsp;&nbsp; System.out.println(&quot;a >>> 2 = &quot; + c );
&nbsp; }
}"></button></div>
</div>
<p>以上实例编译运行结果如下：</p>

<pre class="prettyprint prettyprinted" style=""><span class="pln">a </span><span class="pun">&amp;</span><span class="pln"> b </span><span class="pun">=</span><span class="pln"> </span><span class="lit">12</span><span class="pln">
a </span><span class="pun">|</span><span class="pln"> b </span><span class="pun">=</span><span class="pln"> </span><span class="lit">61</span><span class="pln">
a </span><span class="pun">^</span><span class="pln"> b </span><span class="pun">=</span><span class="pln"> </span><span class="lit">49</span><span class="pln">
</span><span class="pun">~</span><span class="pln">a </span><span class="pun">=</span><span class="pln"> </span><span class="pun">-</span><span class="lit">61</span><span class="pln">
a </span><span class="pun">&lt;&lt;</span><span class="pln"> </span><span class="lit">2</span><span class="pln"> </span><span class="pun">=</span><span class="pln"> </span><span class="lit">240</span><span class="pln">
a </span><span class="pun">&gt;&gt;</span><span class="pln"> </span><span class="lit">2</span><span class="pln">  </span><span class="pun">=</span><span class="pln"> </span><span class="lit">15</span><span class="pln">
a </span><span class="pun">&gt;&gt;&gt;</span><span class="pln"> </span><span class="lit">2</span><span class="pln"> </span><span class="pun">=</span><span class="pln"> </span><span class="lit">15</span><button class="copy-code-button" type="button" data-clipboard-text="a &amp; b = 12
a | b = 61
a ^ b = 49
~a = -61
a << 2 = 240
a >> 2  = 15
a >>> 2 = 15
"></button></pre>
<hr><h2>逻辑运算符</h2>
<p>下表列出了逻辑运算符的基本运算，假设布尔变量A为真，变量B为假</p>
<table class="reference">
<tbody>
    <tr>
        <th style="width:37px;">
            操作符</th>
        <th style="width:433px;">
            描述</th>
        <th style="width:106px;">
            例子</th>
    </tr>
    <tr>
        <td style="width:37px;">
            &amp;&amp;</td>
        <td style="width:433px;">
            称为逻辑与运算符。当且仅当两个操作数都为真，条件才为真。</td>
        <td style="width:106px;">
            （A &amp;&amp; B）为假。</td>
    </tr>
    <tr>
        <td style="width:37px;">
            | |</td>
        <td style="width:433px;">
            称为逻辑或操作符。如果任何两个操作数任何一个为真，条件为真。</td>
        <td style="width:106px;">
            （A | | B）为真。</td>
    </tr>
    <tr>
        <td style="width:37px;">
            ！</td>
        <td style="width:433px;">
            称为逻辑非运算符。用来反转操作数的逻辑状态。如果条件为true，则逻辑非运算符将得到false。</td>
        <td style="width:106px;">
            ！（A &amp;&amp; B）为真。</td>
    </tr>
</tbody>
</table>
<h3>实例</h3>
<p>下面的简单示例程序演示了逻辑运算符。复制并粘贴下面的Java程序并保存为Test.java文件，然后编译并运行这个程序：</p>
<div class="example"> 
<h2 class="example">实例</h2> 
<div class="example_code">
<div class="hl-main"><span class="hl-reserved">public</span><span class="hl-code"> </span><span class="hl-reserved">class</span><span class="hl-code"> </span><span class="hl-identifier">Test</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code">
</span><span class="hl-reserved">public</span><span class="hl-code"> </span><span class="hl-types">static</span><span class="hl-code"> </span><span class="hl-types">void</span><span class="hl-code"> </span><span class="hl-identifier">main</span><span class="hl-brackets">(</span><span class="hl-identifier">String</span><span class="hl-brackets">[</span><span class="hl-brackets">]</span><span class="hl-code"> </span><span class="hl-identifier">args</span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code">
 </span><span class="hl-types">boolean</span><span class="hl-code"> </span><span class="hl-identifier">a</span><span class="hl-code"> = </span><span class="hl-reserved">true</span><span class="hl-code">;
 </span><span class="hl-types">boolean</span><span class="hl-code"> </span><span class="hl-identifier">b</span><span class="hl-code"> = </span><span class="hl-reserved">false</span><span class="hl-code">;
 </span><span class="hl-identifier">System</span><span class="hl-code">.</span><span class="hl-identifier">out</span><span class="hl-code">.</span><span class="hl-identifier">println</span><span class="hl-brackets">(</span><span class="hl-quotes">"</span><span class="hl-string">a &amp;&amp; b = </span><span class="hl-quotes">"</span><span class="hl-code"> + </span><span class="hl-brackets">(</span><span class="hl-identifier">a</span><span class="hl-code">&amp;&amp;</span><span class="hl-identifier">b</span><span class="hl-brackets">)</span><span class="hl-brackets">)</span><span class="hl-code">;
 </span><span class="hl-identifier">System</span><span class="hl-code">.</span><span class="hl-identifier">out</span><span class="hl-code">.</span><span class="hl-identifier">println</span><span class="hl-brackets">(</span><span class="hl-quotes">"</span><span class="hl-string">a || b = </span><span class="hl-quotes">"</span><span class="hl-code"> + </span><span class="hl-brackets">(</span><span class="hl-identifier">a</span><span class="hl-code">||</span><span class="hl-identifier">b</span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-brackets">)</span><span class="hl-code">;
 </span><span class="hl-identifier">System</span><span class="hl-code">.</span><span class="hl-identifier">out</span><span class="hl-code">.</span><span class="hl-identifier">println</span><span class="hl-brackets">(</span><span class="hl-quotes">"</span><span class="hl-string">!(a &amp;&amp; b) = </span><span class="hl-quotes">"</span><span class="hl-code"> + !</span><span class="hl-brackets">(</span><span class="hl-identifier">a</span><span class="hl-code"> &amp;&amp; </span><span class="hl-identifier">b</span><span class="hl-brackets">)</span><span class="hl-brackets">)</span><span class="hl-code">;
</span><span class="hl-brackets">}</span><span class="hl-code">
</span><span class="hl-brackets">}</span></div>
<button class="copy-code-button" type="button" data-clipboard-text="public class Test {
public static void main(String[] args) {
 boolean a = true;
 boolean b = false;
 System.out.println(&quot;a &amp;&amp; b = &quot; + (a&amp;&amp;b));
 System.out.println(&quot;a || b = &quot; + (a||b) );
 System.out.println(&quot;!(a &amp;&amp; b) = &quot; + !(a &amp;&amp; b));
}
}"></button></div>
</div>
<p>以上实例编译运行结果如下：</p>
<pre class="prettyprint prettyprinted" style=""><span class="pln">a </span><span class="pun">&amp;&amp;</span><span class="pln"> b </span><span class="pun">=</span><span class="pln"> </span><span class="kwd">false</span><span class="pln">
a </span><span class="pun">||</span><span class="pln"> b </span><span class="pun">=</span><span class="pln"> </span><span class="kwd">true</span><span class="pln">
</span><span class="pun">!(</span><span class="pln">a </span><span class="pun">&amp;&amp;</span><span class="pln"> b</span><span class="pun">)</span><span class="pln"> </span><span class="pun">=</span><span class="pln"> </span><span class="kwd">true</span><button class="copy-code-button" type="button" data-clipboard-text="a &amp;&amp; b = false
a || b = true
!(a &amp;&amp; b) = true
"></button></pre>
<h3>短路逻辑运算符</h3>
<p>当使用与逻辑运算符时，在两个操作数都为true时，结果才为true，但是当得到第一个操作为false时，其结果就必定是false，这时候就不会再判断第二个操作了。</p>
<div class="example"> 
<h2 class="example">实例</h2> 
<div class="example_code">
<div class="hl-main"><span class="hl-reserved">public</span><span class="hl-code"> </span><span class="hl-reserved">class</span><span class="hl-code"> </span><span class="hl-identifier">LuoJi</span><span class="hl-brackets">{</span><span class="hl-code">
</span><span class="hl-reserved">public</span><span class="hl-code"> </span><span class="hl-types">static</span><span class="hl-code"> </span><span class="hl-types">void</span><span class="hl-code"> </span><span class="hl-identifier">main</span><span class="hl-brackets">(</span><span class="hl-identifier">String</span><span class="hl-brackets">[</span><span class="hl-brackets">]</span><span class="hl-code"> </span><span class="hl-identifier">args</span><span class="hl-brackets">)</span><span class="hl-brackets">{</span><span class="hl-code">
    </span><span class="hl-types">int</span><span class="hl-code"> </span><span class="hl-identifier">a</span><span class="hl-code"> = </span><span class="hl-number">5</span><span class="hl-code">;</span><span class="hl-comment">//</span><span class="hl-comment">定义一个变量；</span><span class="hl-comment"></span><span class="hl-code">
    </span><span class="hl-types">boolean</span><span class="hl-code"> </span><span class="hl-identifier">b</span><span class="hl-code"> = </span><span class="hl-brackets">(</span><span class="hl-identifier">a</span><span class="hl-code">&lt;</span><span class="hl-number">4</span><span class="hl-brackets">)</span><span class="hl-code">&amp;&amp;</span><span class="hl-brackets">(</span><span class="hl-identifier">a</span><span class="hl-code">++&lt;</span><span class="hl-number">10</span><span class="hl-brackets">)</span><span class="hl-code">;
    </span><span class="hl-identifier">System</span><span class="hl-code">.</span><span class="hl-identifier">out</span><span class="hl-code">.</span><span class="hl-identifier">println</span><span class="hl-brackets">(</span><span class="hl-quotes">"</span><span class="hl-string">使用短路逻辑运算符的结果为</span><span class="hl-quotes">"</span><span class="hl-code">+</span><span class="hl-identifier">b</span><span class="hl-brackets">)</span><span class="hl-code">;
    </span><span class="hl-identifier">System</span><span class="hl-code">.</span><span class="hl-identifier">out</span><span class="hl-code">.</span><span class="hl-identifier">println</span><span class="hl-brackets">(</span><span class="hl-quotes">"</span><span class="hl-string">a的结果为</span><span class="hl-quotes">"</span><span class="hl-code">+</span><span class="hl-identifier">a</span><span class="hl-brackets">)</span><span class="hl-code">;
</span><span class="hl-brackets">}</span><span class="hl-code">
</span><span class="hl-brackets">}</span></div>
<button class="copy-code-button" type="button" data-clipboard-text="public class LuoJi{
public static void main(String[] args){
    int a = 5;//定义一个变量；
    boolean b = (a<4)&amp;&amp;(a++<10);
    System.out.println(&quot;使用短路逻辑运算符的结果为&quot;+b);
    System.out.println(&quot;a的结果为&quot;+a);
}
}"></button></div>
</div>
<p>运行结果为：</p>
<pre class="prettyprint prettyprinted" style=""><span class="pun">使用短路逻辑运算符的结果为</span><span class="kwd">false</span><span class="pln">
a</span><span class="pun">的结果为</span><span class="lit">5</span><button class="copy-code-button" type="button" data-clipboard-text="使用短路逻辑运算符的结果为false
a的结果为5"></button></pre>


<blockquote><p><b>解析：
</b>
该程序使用到了短路逻辑运算符(&amp;&amp;)，首先判断 a&lt;4 的结果为 false，则 b 的结果必定是 false，所以不再执行第二个操作 a++&lt;10 的判断，所以 a 的值为 5。
</p></blockquote>


<hr><h2>赋值运算符</h2>
<p>

下面是Java语言支持的赋值运算符：</p>
<table class="reference">
<tbody>
    <tr>
        <th style="width:46px;">
            操作符</th>
        <th style="width:376px;">
            描述</th>
        <th style="width:154px;">
            例子</th>
    </tr>
    <tr>
        <td style="width:46px;">
            =</td>
        <td style="width:376px;">
            简单的赋值运算符，将右操作数的值赋给左侧操作数</td>
        <td style="width:154px;">
            C = A + B将把A + B得到的值赋给C</td>
    </tr>
    <tr>
        <td style="width:46px;">
            + =</td>
        <td style="width:376px;">
            加和赋值操作符，它把左操作数和右操作数相加赋值给左操作数</td>
        <td style="width:154px;">
            C + = A等价于C = C + A</td>
    </tr>
    <tr>
        <td style="width:46px;">
            - =</td>
        <td style="width:376px;">
            减和赋值操作符，它把左操作数和右操作数相减赋值给左操作数</td>
        <td style="width:154px;">
            C - = A等价于C = C - A</td>
    </tr>
    <tr>
        <td style="width:46px;">
            * =</td>
        <td style="width:376px;">
            乘和赋值操作符，它把左操作数和右操作数相乘赋值给左操作数</td>
        <td style="width:154px;">
            C * = A等价于C = C * A</td>
    </tr>
    <tr>
        <td style="width:46px;">
            / =</td>
        <td style="width:376px;">
            除和赋值操作符，它把左操作数和右操作数相除赋值给左操作数</td>
        <td style="width:154px;">
            C / = A，C 与 A 同类型时等价于 C = C / A</td>
    </tr>
    <tr>
        <td style="width:46px;">
            （％）=</td>
        <td style="width:376px;">
            取模和赋值操作符，它把左操作数和右操作数取模后赋值给左操作数</td>
        <td style="width:154px;">
            C％= A等价于C = C％A</td>
    </tr>
    <tr>
        <td style="width:46px;">
            &lt;&lt; =</td>
        <td style="width:376px;">
            左移位赋值运算符</td>
        <td style="width:154px;">
            C &lt;&lt; = 2等价于C = C &lt;&lt; 2</td>
    </tr>
    <tr>
        <td style="width:46px;">
            &gt;&gt; =</td>
        <td style="width:376px;">
            右移位赋值运算符</td>
        <td style="width:154px;">
            C &gt;&gt; = 2等价于C = C &gt;&gt; 2</td>
    </tr>
    <tr>
        <td style="width:46px;">
            ＆=</td>
        <td style="width:376px;">
            按位与赋值运算符</td>
        <td style="width:154px;">
            C＆= 2等价于C = C＆2</td>
    </tr>
    <tr>
        <td style="width:46px;">
            ^ =</td>
        <td style="width:376px;">
            按位异或赋值操作符</td>
        <td style="width:154px;">
            C ^ = 2等价于C = C ^ 2</td>
    </tr>
    <tr>
        <td style="width:46px;">
            | =</td>
        <td style="width:376px;">
            按位或赋值操作符</td>
        <td style="width:154px;">
            C | = 2等价于C = C | 2</td>
    </tr>
</tbody>
</table>
<h3>实例</h3>
<p>下面的简单示例程序演示了赋值运算符。复制并粘贴下面的Java程序并保存为Test.java文件，然后编译并运行这个程序：</p>
<div class="example">
<h2 class="example">Test.java 文件代码：</h2> 
<div class="example_code">
<div class="hl-main"><span class="hl-reserved">public</span><span class="hl-code"> </span><span class="hl-reserved">class</span><span class="hl-code"> </span><span class="hl-identifier">Test</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code">
</span><span class="hl-reserved">public</span><span class="hl-code"> </span><span class="hl-types">static</span><span class="hl-code"> </span><span class="hl-types">void</span><span class="hl-code"> </span><span class="hl-identifier">main</span><span class="hl-brackets">(</span><span class="hl-identifier">String</span><span class="hl-brackets">[</span><span class="hl-brackets">]</span><span class="hl-code"> </span><span class="hl-identifier">args</span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code">
    </span><span class="hl-types">int</span><span class="hl-code"> </span><span class="hl-identifier">a</span><span class="hl-code"> = </span><span class="hl-number">10</span><span class="hl-code">;
    </span><span class="hl-types">int</span><span class="hl-code"> </span><span class="hl-identifier">b</span><span class="hl-code"> = </span><span class="hl-number">20</span><span class="hl-code">;
    </span><span class="hl-types">int</span><span class="hl-code"> </span><span class="hl-identifier">c</span><span class="hl-code"> = </span><span class="hl-number">0</span><span class="hl-code">;
    </span><span class="hl-identifier">c</span><span class="hl-code"> = </span><span class="hl-identifier">a</span><span class="hl-code"> + </span><span class="hl-identifier">b</span><span class="hl-code">;
    </span><span class="hl-identifier">System</span><span class="hl-code">.</span><span class="hl-identifier">out</span><span class="hl-code">.</span><span class="hl-identifier">println</span><span class="hl-brackets">(</span><span class="hl-quotes">"</span><span class="hl-string">c = a + b = </span><span class="hl-quotes">"</span><span class="hl-code"> + </span><span class="hl-identifier">c</span><span class="hl-code"> </span><span class="hl-brackets">)</span><span class="hl-code">;
    </span><span class="hl-identifier">c</span><span class="hl-code"> += </span><span class="hl-identifier">a</span><span class="hl-code"> ;
    </span><span class="hl-identifier">System</span><span class="hl-code">.</span><span class="hl-identifier">out</span><span class="hl-code">.</span><span class="hl-identifier">println</span><span class="hl-brackets">(</span><span class="hl-quotes">"</span><span class="hl-string">c += a&nbsp; = </span><span class="hl-quotes">"</span><span class="hl-code"> + </span><span class="hl-identifier">c</span><span class="hl-code"> </span><span class="hl-brackets">)</span><span class="hl-code">;
    </span><span class="hl-identifier">c</span><span class="hl-code"> -= </span><span class="hl-identifier">a</span><span class="hl-code"> ;
    </span><span class="hl-identifier">System</span><span class="hl-code">.</span><span class="hl-identifier">out</span><span class="hl-code">.</span><span class="hl-identifier">println</span><span class="hl-brackets">(</span><span class="hl-quotes">"</span><span class="hl-string">c -= a = </span><span class="hl-quotes">"</span><span class="hl-code"> + </span><span class="hl-identifier">c</span><span class="hl-code"> </span><span class="hl-brackets">)</span><span class="hl-code">;
    </span><span class="hl-identifier">c</span><span class="hl-code"> *= </span><span class="hl-identifier">a</span><span class="hl-code"> ;
    </span><span class="hl-identifier">System</span><span class="hl-code">.</span><span class="hl-identifier">out</span><span class="hl-code">.</span><span class="hl-identifier">println</span><span class="hl-brackets">(</span><span class="hl-quotes">"</span><span class="hl-string">c *= a = </span><span class="hl-quotes">"</span><span class="hl-code"> + </span><span class="hl-identifier">c</span><span class="hl-code"> </span><span class="hl-brackets">)</span><span class="hl-code">;
    </span><span class="hl-identifier">a</span><span class="hl-code"> = </span><span class="hl-number">10</span><span class="hl-code">;
    </span><span class="hl-identifier">c</span><span class="hl-code"> = </span><span class="hl-number">15</span><span class="hl-code">;
    </span><span class="hl-identifier">c</span><span class="hl-code"> /= </span><span class="hl-identifier">a</span><span class="hl-code"> ;
    </span><span class="hl-identifier">System</span><span class="hl-code">.</span><span class="hl-identifier">out</span><span class="hl-code">.</span><span class="hl-identifier">println</span><span class="hl-brackets">(</span><span class="hl-quotes">"</span><span class="hl-string">c /= a = </span><span class="hl-quotes">"</span><span class="hl-code"> + </span><span class="hl-identifier">c</span><span class="hl-code"> </span><span class="hl-brackets">)</span><span class="hl-code">;
    </span><span class="hl-identifier">a</span><span class="hl-code"> = </span><span class="hl-number">10</span><span class="hl-code">;
    </span><span class="hl-identifier">c</span><span class="hl-code"> = </span><span class="hl-number">15</span><span class="hl-code">;
    </span><span class="hl-identifier">c</span><span class="hl-code"> %= </span><span class="hl-identifier">a</span><span class="hl-code"> ;
    </span><span class="hl-identifier">System</span><span class="hl-code">.</span><span class="hl-identifier">out</span><span class="hl-code">.</span><span class="hl-identifier">println</span><span class="hl-brackets">(</span><span class="hl-quotes">"</span><span class="hl-string">c %= a&nbsp; = </span><span class="hl-quotes">"</span><span class="hl-code"> + </span><span class="hl-identifier">c</span><span class="hl-code"> </span><span class="hl-brackets">)</span><span class="hl-code">;
    </span><span class="hl-identifier">c</span><span class="hl-code"> &lt;&lt;= </span><span class="hl-number">2</span><span class="hl-code"> ;
    </span><span class="hl-identifier">System</span><span class="hl-code">.</span><span class="hl-identifier">out</span><span class="hl-code">.</span><span class="hl-identifier">println</span><span class="hl-brackets">(</span><span class="hl-quotes">"</span><span class="hl-string">c &lt;&lt;= 2 = </span><span class="hl-quotes">"</span><span class="hl-code"> + </span><span class="hl-identifier">c</span><span class="hl-code"> </span><span class="hl-brackets">)</span><span class="hl-code">;
    </span><span class="hl-identifier">c</span><span class="hl-code"> &gt;&gt;= </span><span class="hl-number">2</span><span class="hl-code"> ;
    </span><span class="hl-identifier">System</span><span class="hl-code">.</span><span class="hl-identifier">out</span><span class="hl-code">.</span><span class="hl-identifier">println</span><span class="hl-brackets">(</span><span class="hl-quotes">"</span><span class="hl-string">c &gt;&gt;= 2 = </span><span class="hl-quotes">"</span><span class="hl-code"> + </span><span class="hl-identifier">c</span><span class="hl-code"> </span><span class="hl-brackets">)</span><span class="hl-code">;
    </span><span class="hl-identifier">c</span><span class="hl-code"> &gt;&gt;= </span><span class="hl-number">2</span><span class="hl-code"> ;
    </span><span class="hl-identifier">System</span><span class="hl-code">.</span><span class="hl-identifier">out</span><span class="hl-code">.</span><span class="hl-identifier">println</span><span class="hl-brackets">(</span><span class="hl-quotes">"</span><span class="hl-string">c &gt;&gt;= 2 = </span><span class="hl-quotes">"</span><span class="hl-code"> + </span><span class="hl-identifier">c</span><span class="hl-code"> </span><span class="hl-brackets">)</span><span class="hl-code">;
    </span><span class="hl-identifier">c</span><span class="hl-code"> &amp;= </span><span class="hl-identifier">a</span><span class="hl-code"> ;
    </span><span class="hl-identifier">System</span><span class="hl-code">.</span><span class="hl-identifier">out</span><span class="hl-code">.</span><span class="hl-identifier">println</span><span class="hl-brackets">(</span><span class="hl-quotes">"</span><span class="hl-string">c &amp;= a&nbsp; = </span><span class="hl-quotes">"</span><span class="hl-code"> + </span><span class="hl-identifier">c</span><span class="hl-code"> </span><span class="hl-brackets">)</span><span class="hl-code">;
    </span><span class="hl-identifier">c</span><span class="hl-code"> ^= </span><span class="hl-identifier">a</span><span class="hl-code"> ;
    </span><span class="hl-identifier">System</span><span class="hl-code">.</span><span class="hl-identifier">out</span><span class="hl-code">.</span><span class="hl-identifier">println</span><span class="hl-brackets">(</span><span class="hl-quotes">"</span><span class="hl-string">c ^= a&nbsp;&nbsp; = </span><span class="hl-quotes">"</span><span class="hl-code"> + </span><span class="hl-identifier">c</span><span class="hl-code"> </span><span class="hl-brackets">)</span><span class="hl-code">;
    </span><span class="hl-identifier">c</span><span class="hl-code"> |= </span><span class="hl-identifier">a</span><span class="hl-code"> ;
    </span><span class="hl-identifier">System</span><span class="hl-code">.</span><span class="hl-identifier">out</span><span class="hl-code">.</span><span class="hl-identifier">println</span><span class="hl-brackets">(</span><span class="hl-quotes">"</span><span class="hl-string">c |= a&nbsp;&nbsp; = </span><span class="hl-quotes">"</span><span class="hl-code"> + </span><span class="hl-identifier">c</span><span class="hl-code"> </span><span class="hl-brackets">)</span><span class="hl-code">;
</span><span class="hl-brackets">}</span><span class="hl-code">
</span><span class="hl-brackets">}</span></div>
<button class="copy-code-button" type="button" data-clipboard-text="public class Test {
public static void main(String[] args) {
    int a = 10;
    int b = 20;
    int c = 0;
    c = a + b;
    System.out.println(&quot;c = a + b = &quot; + c );
    c += a ;
    System.out.println(&quot;c += a&nbsp; = &quot; + c );
    c -= a ;
    System.out.println(&quot;c -= a = &quot; + c );
    c *= a ;
    System.out.println(&quot;c *= a = &quot; + c );
    a = 10;
    c = 15;
    c /= a ;
    System.out.println(&quot;c /= a = &quot; + c );
    a = 10;
    c = 15;
    c %= a ;
    System.out.println(&quot;c %= a&nbsp; = &quot; + c );
    c <<= 2 ;
    System.out.println(&quot;c <<= 2 = &quot; + c );
    c >>= 2 ;
    System.out.println(&quot;c >>= 2 = &quot; + c );
    c >>= 2 ;
    System.out.println(&quot;c >>= 2 = &quot; + c );
    c &amp;= a ;
    System.out.println(&quot;c &amp;= a&nbsp; = &quot; + c );
    c ^= a ;
    System.out.println(&quot;c ^= a&nbsp;&nbsp; = &quot; + c );
    c |= a ;
    System.out.println(&quot;c |= a&nbsp;&nbsp; = &quot; + c );
}
}"></button></div>
</div>
<p>以上实例编译运行结果如下：</p>
<pre class="prettyprint prettyprinted" style=""><span class="pln">c </span><span class="pun">=</span><span class="pln"> a </span><span class="pun">+</span><span class="pln"> b </span><span class="pun">=</span><span class="pln"> </span><span class="lit">30</span><span class="pln">
c </span><span class="pun">+=</span><span class="pln"> a&nbsp; </span><span class="pun">=</span><span class="pln"> </span><span class="lit">40</span><span class="pln">
c </span><span class="pun">-=</span><span class="pln"> a </span><span class="pun">=</span><span class="pln"> </span><span class="lit">30</span><span class="pln">
c </span><span class="pun">*=</span><span class="pln"> a </span><span class="pun">=</span><span class="pln"> </span><span class="lit">300</span><span class="pln">
c </span><span class="pun">/=</span><span class="pln"> a </span><span class="pun">=</span><span class="pln"> </span><span class="lit">1</span><span class="pln">
c </span><span class="pun">%=</span><span class="pln"> a&nbsp; </span><span class="pun">=</span><span class="pln"> </span><span class="lit">5</span><span class="pln">
c </span><span class="pun">&lt;&lt;=</span><span class="pln"> </span><span class="lit">2</span><span class="pln"> </span><span class="pun">=</span><span class="pln"> </span><span class="lit">20</span><span class="pln">
c </span><span class="pun">&gt;&gt;=</span><span class="pln"> </span><span class="lit">2</span><span class="pln"> </span><span class="pun">=</span><span class="pln"> </span><span class="lit">5</span><span class="pln">
c </span><span class="pun">&gt;&gt;=</span><span class="pln"> </span><span class="lit">2</span><span class="pln"> </span><span class="pun">=</span><span class="pln"> </span><span class="lit">1</span><span class="pln">
c </span><span class="pun">&amp;=</span><span class="pln"> a&nbsp; </span><span class="pun">=</span><span class="pln"> </span><span class="lit">0</span><span class="pln">
c </span><span class="pun">^=</span><span class="pln"> a&nbsp;&nbsp; </span><span class="pun">=</span><span class="pln"> </span><span class="lit">10</span><span class="pln">
c </span><span class="pun">|=</span><span class="pln"> a&nbsp;&nbsp; </span><span class="pun">=</span><span class="pln"> </span><span class="lit">10</span><button class="copy-code-button" type="button" data-clipboard-text="c = a + b = 30
c += a&nbsp; = 40
c -= a = 30
c *= a = 300
c /= a = 1
c %= a&nbsp; = 5
c <<= 2 = 20
c >>= 2 = 5
c >>= 2 = 1
c &amp;= a&nbsp; = 0
c ^= a&nbsp;&nbsp; = 10
c |= a&nbsp;&nbsp; = 10
"></button></pre>
<hr><h2>条件运算符（?:）</h2>
<p>条件运算符也被称为三元运算符。该运算符有3个操作数，并且需要判断布尔表达式的值。该运算符的主要是决定哪个值应该赋值给变量。</p>
<pre class="prettyprint prettyprinted" style=""><span class="pln">variable x </span><span class="pun">=</span><span class="pln"> </span><span class="pun">(</span><span class="pln">expression</span><span class="pun">)</span><span class="pln"> </span><span class="pun">?</span><span class="pln"> value </span><span class="kwd">if</span><span class="pln"> </span><span class="kwd">true</span><span class="pln"> </span><span class="pun">:</span><span class="pln"> value </span><span class="kwd">if</span><span class="pln"> </span><span class="kwd">false</span><button class="copy-code-button" type="button" data-clipboard-text="variable x = (expression) ? value if true : value if false
"></button></pre>
<h3>实例</h3>
<div class="example">
<h2 class="example">Test.java 文件代码：</h2> 
<div class="example_code">
<div class="hl-main"><span class="hl-reserved">public</span><span class="hl-code"> </span><span class="hl-reserved">class</span><span class="hl-code"> </span><span class="hl-identifier">Test</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code">
</span><span class="hl-reserved">public</span><span class="hl-code"> </span><span class="hl-types">static</span><span class="hl-code"> </span><span class="hl-types">void</span><span class="hl-code"> </span><span class="hl-identifier">main</span><span class="hl-brackets">(</span><span class="hl-identifier">String</span><span class="hl-brackets">[</span><span class="hl-brackets">]</span><span class="hl-code"> </span><span class="hl-identifier">args</span><span class="hl-brackets">)</span><span class="hl-brackets">{</span><span class="hl-code">
  </span><span class="hl-types">int</span><span class="hl-code"> </span><span class="hl-identifier">a</span><span class="hl-code"> , </span><span class="hl-identifier">b</span><span class="hl-code">;
  </span><span class="hl-identifier">a</span><span class="hl-code"> = </span><span class="hl-number">10</span><span class="hl-code">;
  </span><span class="hl-comment">//</span><span class="hl-comment"> 如果 a 等于 1 成立，则设置 b 为 20，否则为 30</span><span class="hl-comment"></span><span class="hl-code">
  </span><span class="hl-identifier">b</span><span class="hl-code"> = </span><span class="hl-brackets">(</span><span class="hl-identifier">a</span><span class="hl-code"> == </span><span class="hl-number">1</span><span class="hl-brackets">)</span><span class="hl-code"> ? </span><span class="hl-number">20</span><span class="hl-code"> : </span><span class="hl-number">30</span><span class="hl-code">;
  </span><span class="hl-identifier">System</span><span class="hl-code">.</span><span class="hl-identifier">out</span><span class="hl-code">.</span><span class="hl-identifier">println</span><span class="hl-brackets">(</span><span class="hl-code"> </span><span class="hl-quotes">"</span><span class="hl-string">Value of b is : </span><span class="hl-quotes">"</span><span class="hl-code"> +  </span><span class="hl-identifier">b</span><span class="hl-code"> </span><span class="hl-brackets">)</span><span class="hl-code">;

  </span><span class="hl-comment">//</span><span class="hl-comment"> 如果 a 等于 10 成立，则设置 b 为 20，否则为 30</span><span class="hl-comment"></span><span class="hl-code">
  </span><span class="hl-identifier">b</span><span class="hl-code"> = </span><span class="hl-brackets">(</span><span class="hl-identifier">a</span><span class="hl-code"> == </span><span class="hl-number">10</span><span class="hl-brackets">)</span><span class="hl-code"> ? </span><span class="hl-number">20</span><span class="hl-code"> : </span><span class="hl-number">30</span><span class="hl-code">;
  </span><span class="hl-identifier">System</span><span class="hl-code">.</span><span class="hl-identifier">out</span><span class="hl-code">.</span><span class="hl-identifier">println</span><span class="hl-brackets">(</span><span class="hl-code"> </span><span class="hl-quotes">"</span><span class="hl-string">Value of b is : </span><span class="hl-quotes">"</span><span class="hl-code"> + </span><span class="hl-identifier">b</span><span class="hl-code"> </span><span class="hl-brackets">)</span><span class="hl-code">;
</span><span class="hl-brackets">}</span><span class="hl-code">
</span><span class="hl-brackets">}</span></div>
<button class="copy-code-button" type="button" data-clipboard-text="public class Test {
public static void main(String[] args){
  int a , b;
  a = 10;
  // 如果 a 等于 1 成立，则设置 b 为 20，否则为 30
  b = (a == 1) ? 20 : 30;
  System.out.println( &quot;Value of b is : &quot; +  b );

  // 如果 a 等于 10 成立，则设置 b 为 20，否则为 30
  b = (a == 10) ? 20 : 30;
  System.out.println( &quot;Value of b is : &quot; + b );
}
}"></button></div>
</div>
<p>以上实例编译运行结果如下：</p>
<pre class="prettyprint prettyprinted" style=""><span class="typ">Value</span><span class="pln"> of b </span><span class="kwd">is</span><span class="pln"> </span><span class="pun">:</span><span class="pln"> </span><span class="lit">30</span><span class="pln">
</span><span class="typ">Value</span><span class="pln"> of b </span><span class="kwd">is</span><span class="pln"> </span><span class="pun">:</span><span class="pln"> </span><span class="lit">20</span><button class="copy-code-button" type="button" data-clipboard-text="Value of b is : 30
Value of b is : 20
"></button></pre>

<hr><h2>instanceof 运算符</h2>

<p>该运算符用于操作对象实例，检查该对象是否是一个特定类型（类类型或接口类型）。</p>
<p>instanceof运算符使用格式如下：</p>
<pre class="prettyprint prettyprinted" style=""><span class="pun">(</span><span class="pln"> </span><span class="typ">Object</span><span class="pln"> reference variable </span><span class="pun">)</span><span class="pln"> </span><span class="kwd">instanceof</span><span class="pln">  </span><span class="pun">(</span><span class="kwd">class</span><span class="pun">/</span><span class="kwd">interface</span><span class="pln"> type</span><span class="pun">)</span><button class="copy-code-button" type="button" data-clipboard-text="( Object reference variable ) instanceof  (class/interface type)
"></button></pre>
<p>
如果运算符左侧变量所指的对象，是操作符右侧类或接口(class/interface)的一个对象，那么结果为真。
</p><p>
下面是一个例子：</p>
<pre class="prettyprint prettyprinted" style=""><span class="typ">String</span><span class="pln"> name </span><span class="pun">=</span><span class="pln"> </span><span class="str">"James"</span><span class="pun">;</span><span class="pln">
</span><span class="kwd">boolean</span><span class="pln"> result </span><span class="pun">=</span><span class="pln"> name </span><span class="kwd">instanceof</span><span class="pln"> </span><span class="typ">String</span><span class="pun">;</span><span class="pln"> </span><span class="com">// 由于 name 是 String 类型，所以返回真</span><button class="copy-code-button" type="button" data-clipboard-text="String name = &quot;James&quot;;
boolean result = name instanceof String; // 由于 name 是 String 类型，所以返回真
"></button></pre>
<p>如果被比较的对象兼容于右侧类型，该运算符仍然返回 true。</p>
<p>看下面的例子：</p>
<div class="example">
<div class="example_code">
<div class="hl-main"><span class="hl-reserved">class</span><span class="hl-code"> </span><span class="hl-identifier">Vehicle</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-brackets">}</span><span class="hl-code">

</span><span class="hl-reserved">public</span><span class="hl-code"> </span><span class="hl-reserved">class</span><span class="hl-code"> </span><span class="hl-identifier">Car</span><span class="hl-code"> </span><span class="hl-reserved">extends</span><span class="hl-code"> </span><span class="hl-identifier">Vehicle</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code">
</span><span class="hl-reserved">public</span><span class="hl-code"> </span><span class="hl-types">static</span><span class="hl-code"> </span><span class="hl-types">void</span><span class="hl-code"> </span><span class="hl-identifier">main</span><span class="hl-brackets">(</span><span class="hl-identifier">String</span><span class="hl-brackets">[</span><span class="hl-brackets">]</span><span class="hl-code"> </span><span class="hl-identifier">args</span><span class="hl-brackets">)</span><span class="hl-brackets">{</span><span class="hl-code">
  </span><span class="hl-identifier">Vehicle</span><span class="hl-code"> </span><span class="hl-identifier">a</span><span class="hl-code"> = </span><span class="hl-reserved">new</span><span class="hl-code"> </span><span class="hl-identifier">Car</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">;
  </span><span class="hl-types">boolean</span><span class="hl-code"> </span><span class="hl-identifier">result</span><span class="hl-code"> =  </span><span class="hl-identifier">a</span><span class="hl-code"> </span><span class="hl-reserved">instanceof</span><span class="hl-code"> </span><span class="hl-identifier">Car</span><span class="hl-code">;
  </span><span class="hl-identifier">System</span><span class="hl-code">.</span><span class="hl-identifier">out</span><span class="hl-code">.</span><span class="hl-identifier">println</span><span class="hl-brackets">(</span><span class="hl-code"> </span><span class="hl-identifier">result</span><span class="hl-brackets">)</span><span class="hl-code">;
</span><span class="hl-brackets">}</span><span class="hl-code">
</span><span class="hl-brackets">}</span></div>
<button class="copy-code-button" type="button" data-clipboard-text="class Vehicle {}

public class Car extends Vehicle {
public static void main(String[] args){
  Vehicle a = new Car();
  boolean result =  a instanceof Car;
  System.out.println( result);
}
}"></button></div>
</div>
<p>以上实例编译运行结果如下：</p>
<pre class="prettyprint prettyprinted" style=""><span class="kwd">true</span><button class="copy-code-button" type="button" data-clipboard-text="true"></button></pre>
<hr><h2>Java运算符优先级</h2>
<p>当多个运算符出现在一个表达式中，谁先谁后呢？这就涉及到运算符的优先级别的问题。在一个多运算符的表达式中，运算符优先级不同会导致最后得出的结果差别甚大。</p><p>例如，（1+3）＋（3+2）*2，这个表达式如果按加号最优先计算，答案就是 18，如果按照乘号最优先，答案则是 14。
</p><p>再如，x = 7 + 3 * 2;这里x得到13，而不是20，因为乘法运算符比加法运算符有较高的优先级，所以先计算3 * 2得到6，然后再加7。
</p><p>下表中具有最高优先级的运算符在的表的最上面，最低优先级的在表的底部。</p>
<table class="reference">
<tbody>
    <tr>
        <th style="width:66px;">
            类别 </th>
        <th style="width:274px;">
            操作符</th>
        <th style="width:132px;">
            关联性 </th>
    </tr>
    <tr>
        <td style="width:66px;">
            后缀</td>
        <td style="width:274px;">
            () [] . (点操作符)</td>
        <td style="width:132px;">
            左到右</td>
    </tr>
<tr>
        <td style="width:66px;">
            一元</td>
        <td style="width:274px;">
            expr++  expr--</td>
        <td style="width:132px;">
            从左到右</td>
    </tr>
    <tr>
        <td style="width:66px;">
            一元</td>
        <td style="width:274px;">
            ++expr --expr + - ～ ！</td>
        <td style="width:132px;">
            从右到左</td>
    </tr>
    <tr>
        <td style="width:66px;">
            乘性&nbsp;</td>
        <td style="width:274px;">
            * /％</td>
        <td style="width:132px;">
            左到右</td>
    </tr>
    <tr>
        <td style="width:66px;">
            加性&nbsp;</td>
        <td style="width:274px;">
            + -</td>
        <td style="width:132px;">
            左到右</td>
    </tr>
    <tr>
        <td style="width:66px;">
            移位&nbsp;</td>
        <td style="width:274px;">
            &gt;&gt; &gt;&gt;&gt; &nbsp;&lt;&lt;&nbsp;</td>
        <td style="width:132px;">
            左到右</td>
    </tr>
    <tr>
        <td style="width:66px;">
            关系&nbsp;</td>
        <td style="width:274px;">
            &gt;  &gt;=  &lt;  &lt;=&nbsp;</td>
        <td style="width:132px;">
            左到右</td>
    </tr>
    <tr>
        <td style="width:66px;">
            相等&nbsp;</td>
        <td style="width:274px;">
            ==&nbsp; !=</td>
        <td style="width:132px;">
            左到右</td>
    </tr>
    <tr>
        <td style="width:66px;">
            按位与</td>
        <td style="width:274px;">
            ＆</td>
        <td style="width:132px;">
            左到右</td>
    </tr>
    <tr>
        <td style="width:66px;">
            按位异或</td>
        <td style="width:274px;">
            ^</td>
        <td style="width:132px;">
            左到右</td>
    </tr>
    <tr>
        <td style="width:66px;">
            按位或</td>
        <td style="width:274px;">
            |</td>
        <td style="width:132px;">
            左到右</td>
    </tr>
    <tr>
        <td style="width:66px;">
            逻辑与</td>
        <td style="width:274px;">
            &amp;&amp;</td>
        <td style="width:132px;">
            左到右</td>
    </tr>
    <tr>
        <td style="width:66px;">
            逻辑或</td>
        <td style="width:274px;">
            | |</td>
        <td style="width:132px;">
            左到右</td>
    </tr>
    <tr>
        <td style="width:66px;">
            条件</td>
        <td style="width:274px;">
            ？：</td>
        <td style="width:132px;">
            从右到左</td>
    </tr>
    <tr>
        <td style="width:66px;">
            赋值</td>
        <td style="width:274px;">
            = + = - = * = / =％= &gt;&gt; = &lt;&lt; =＆= ^ = | =</td>
        <td style="width:132px;">
            从右到左</td>
    </tr>
    <tr>
        <td style="width:66px;">
            逗号</td>
        <td style="width:274px;">
            ，</td>
        <td style="width:132px;">
            左到右</td>
    </tr>
</tbody>
</table>			<!-- 其他扩展 -->
                    
        </div>
</body>
</html>